import request from './request'

// 学员相关API接口

export interface Student {
  id: number
  name: string
  gender: string
  phone: string
  email?: string
  province?: string
  source?: string
  status: string
  created_at?: string
  updated_at?: string
}

export interface StudentListResponse {
  items: Student[]
  total: number
  page: number
  size: number
}

export interface StudentDetail {
  student: {
    id: number
    name: string
    gender: string
    phone: string
    email?: string
    province?: string
    source?: string
    status: string
    created_at?: string
    updated_at?: string
  }
  orders: Array<{
    id: number
    order_no: string
    course: {
      id: number
      name: string
      total_stages: number
      total_price: number
      description?: string
      status: string
      subject?: {
        id: number
        name: string
      }
    }
    stages: Array<{
      id: number
      stage_number: number
      stage_name: string
      stage_price: number
      description?: string
    }>
    payment_method?: string
    registration_type: string
    should_pay_amount: number
    actual_pay_amount: number
    order_date?: string
    status: string
    sales_employee?: {
      id: number
      name: string
      employee_no: string
    }
    reception_employee?: {
      id: number
      name: string
      employee_no: string
    }
    performance_source?: string
  }>
  statistics: {
    total_orders: number
    total_should_pay: number
    total_paid: number
    unpaid_amount: number
  }
}

/**
 * 获取学员列表
 */
export const getStudents = (params?: {
  page?: number
  size?: number
  name?: string
  phone?: string
  status?: string
}): Promise<StudentListResponse> => {
  return request.get('/students/', { params })
}

/**
 * 获取学员详细信息
 */
export const getStudentDetail = (studentId: number): Promise<StudentDetail> => {
  return request.get(`/students/${studentId}/detail`)
}

/**
 * 创建学员
 */
export const createStudent = (data: Partial<Student>): Promise<Student> => {
  return request.post('/students/', data)
}

/**
 * 更新学员
 */
export const updateStudent = (id: number, data: Partial<Student>): Promise<Student> => {
  return request.put(`/students/${id}`, data)
}

/**
 * 删除学员
 */
export const deleteStudent = (id: number): Promise<void> => {
  return request.delete(`/students/${id}`)
}

/**
 * 获取学员报名的所有课程
 */
export const getStudentCourses = (studentId: number) => {
  return request.get(`/students/${studentId}/courses`)
}

/**
 * 获取学员特定课程的阶段信息
 */
export const getStudentCourseStages = (studentId: number, courseId: number) => {
  return request.get(`/students/${studentId}/stages/${courseId}`)
}
